<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleleaf.org">
<head th:insert="base">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form" action="" method="post">
            <div class="layui-form-item">
                <label for="switchCode" class="layui-form-label">
                    道岔编号
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="switchCode" name="switchCode" minlength="3" maxlength="20" lay-verify="range"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="switchModel" class="layui-form-label">
                    道岔型号
                </label>
                <div class="layui-input-inline">
                    <select id="switchModel" name="switchModel" class="valid" lay-verify="required">
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="crossCode" class="layui-form-label">
                    交叉设备编号
                </label>
                <div class="layui-input-inline">
                    <select id="crossCode" name="crossCode" class="valid" lay-verify="required" lay-filter ="crossCode">
                    </select>
                    <input type="hidden" id="crossModel" name="crossModel"/>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="connectCode" class="layui-form-label">
                    连接设备编号
                </label>
                <div class="layui-input-inline">
                    <select id="connectCode" name="connectCode" class="valid" lay-verify="required" lay-filter ="connectCode">
                    </select>
                    <input type="hidden" id="connectModel" name="connectModel"/>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="combinationCode" class="layui-form-label">
                    组合设备编号
                </label>
                <div class="layui-input-inline">
                    <select id="combinationCode" name="combinationCode" class="valid" lay-verify="required" lay-filter ="combinationCode">
                    </select>
                    <input type="hidden" id="combinationModel" name="combinationModel"/>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label for="remark" class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <textarea placeholder="请输入内容" id="remark" name="remark" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                </label>
                <button  class="layui-btn" lay-filter="add" lay-submit="">
                    确认新增
                </button>
            </div>
        </form>
    </div>
</div>
<script th:inline="none">
    layui.use(['form','jquery','validate','tool','businessUtil'], function(){
        var form = layui.form;
        var $ = layui.jquery;
        var validate = layui.validate;
        var tool = layui.tool;
        var baseUrl = "/back/switchInfo";
        var businessUtil = layui.businessUtil;

        init_form();

        /**
         * 初始化页面
         */
        function init_form() {
            businessUtil.createDeviceModel('switchModel','3',null,'请选择道岔型号');
            businessUtil.createDeviceInfo('connectCode','0',null,'请选择连接设备编号');
            businessUtil.createDeviceInfo('crossCode','1',null,'请选择交叉设备编号');
            businessUtil.createDeviceInfo('combinationCode','2',null,'请选择组合设备编号');
            form.render();//重新渲染
        }
        //添加表单校验
        form.verify(validate);

        form.on('select(crossCode)', function (data) {
            $("#crossModel").val(data.elem[data.elem.selectedIndex].getAttribute('devicemodel'));
        });
        form.on('select(connectCode)', function (data) {
            $("#connectModel").val(data.elem[data.elem.selectedIndex].getAttribute('devicemodel'));
        });
        form.on('select(combinationCode)', function (data) {
            $("#combinationModel").val(data.elem[data.elem.selectedIndex].getAttribute('devicemodel'));
        });

        //监听提交
        form.on('submit(add)', function(data){
            var res = tool.submit(baseUrl + '/add',data.field);
            if(res.code == return_200){
                layer.msg(res.msg, {
                        icon: msg.success,
                        time: 2000
                    },
                    function(){
                        xadmin.father_reload();
                    }
                );
            }else {
                layer.msg(res.msg, {
                        icon: msg.error,
                        time: 2000
                    },
                    function(){
                        xadmin.father_reload();
                    }
                );
            }
            return false;
        });
        
    });
</script>
</body>
</html>